﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MensUnoShop.Models
{
    public class Tool
    {
        #region 字串處理--取得限定字串長度(全半形)
        public static string Substring(string oristr, int length)
        {
            int pos = 0;
            double sngTotalLength = 0.0;
            try
            {
                if (oristr == string.Empty || length <= 0 || (oristr.Length <= length))
                    return oristr;
                else
                {
                    foreach (var c in oristr.ToCharArray())
                    {
                        if (Convert.ToInt32(c).ToString("x").Length == 2)
                            sngTotalLength += 0.5;
                        else
                            sngTotalLength += 1;

                        if (sngTotalLength + 0.5 <= length)
                            pos += 1;
                        else
                            break;
                    }
                    return oristr.Substring(0, pos);
                }
            }
            catch
            {
                return oristr;
            }
        }
        #endregion
    }
}